package com.parser;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hronline.Constant;
import com.models.Absence;
import com.models.AbsenceSummary;
import com.models.AbsenceType;
import com.models.DefaultStartEndTime;
import com.models.EmergencyContact;
import com.models.Employees;
import com.models.HolidayYear;
import com.models.Item;
import com.models.ManagerSummary;
import com.models.Notif;
import com.models.ReasonLateness;
import com.models.Status;

public class ManagerParser {

	/* =====================================================================
	 * ==========================DASHBOARD PAGE=============================
	 * =====================================================================*/
	/* this method is used to parse manager summary data */
	public static ArrayList<Object> parseManagerSummary(String result){
		
		ArrayList<Object> arrResult	=	new ArrayList<Object>();
		try {
			
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			
			JSONObject jsonModel	=	jsonResult.getJSONObject("Model");
			int absenceRequest		=	jsonModel.getInt("AbsenceRequests");
			int absentToday			=	jsonModel.getInt("AbsentToday");
			int openSickness		=	jsonModel.getInt("OpenSicknesses");
			
			ManagerSummary managerSummary	=	new ManagerSummary();
			managerSummary.setAbsenceRequest(String.format("%d", absenceRequest));
			managerSummary.setAbsentToday(String.format("%d", absentToday));
			managerSummary.setOpenSickness(String.format("%d", openSickness));
			
			arrResult.add(managerSummary);
					
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* this method is used to parse today notification data */
	public static ArrayList<Object> parseTodayNotification(String result){
		
		ArrayList<Object> arrResult	=	new ArrayList<Object>();
		try {
			
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONArray jsonModel	=	jsonResult.getJSONArray("Model");
			ArrayList<Item> arrNotification	=	new ArrayList<Item>();
			for(int index = 0; index < jsonModel.length(); index++){
				JSONObject item =	jsonModel.getJSONObject(index);
				
				String description	=	item.getString("Description");
				int type			=	item.getInt("Type");
				String absenceId	=	item.getString("Id");
				
				Notif notif	=	new Notif();
				notif.setDescription(description);
				notif.setType(type);
				notif.setAbsenceId(absenceId);
				
				arrNotification.add(notif);
			}
			
			arrResult.add(arrNotification);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* =====================================================================
	 * ==========================EMPLOYEES PAGE=============================
	 * =====================================================================*/
	/* this method is used to parse all employees data */
	public static ArrayList<Object> parseAllEmployees(String result){
		
		ArrayList<Object> arrResult	=	new ArrayList<Object>();
		
		try {
			
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONArray jsonModel		=	jsonResult.getJSONArray("Model");
			ArrayList<Employees> arrEmployees	=	new ArrayList<Employees>();
			for(int index = 0; index < jsonModel.length(); index++){
				JSONObject item	=	jsonModel.getJSONObject(index);
				
				String 	email			=	item.getString("Email");
				String	foreName		=	item.getString("Forename");
				String	homeNumber		=	item.getString("HomeNumber");
				String 	id				=	item.getString("Id");
				String	jobTitle		=	item.getString("JobTitle");
				String	mobileNumber	=	item.getString("MobileNumber");
				String	orgUnitName		=	item.getString("OrgUnitName");
				String	startDate		=	item.getString("StartDate");
				String	surName			=	item.getString("Surname");
				String	workNumber		=	item.getString("WorkNumber");
				
				Employees emp			=	new Employees();
				emp.setEmail(email);
				emp.setForeName(foreName);
				emp.setHomeNumber(homeNumber);
				emp.setEmpId(id);
				emp.setJobTitle(jobTitle);
				emp.setMobileNumber(mobileNumber);
				emp.setOrgUnitName(orgUnitName);
				emp.setStartDate(startDate);
				emp.setSurName(surName);
				emp.setWorkNumber(workNumber);
				
				arrEmployees.add(emp);
			}
			
			arrResult.add(arrEmployees);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* =====================================================================
	 * ==========================EMPLOYEE DETAILS PAGE======================
	 * =====================================================================*/
	
	/* this method is used to parse summary absence */
	public static ArrayList<Object> parseEmployeeSummaryAbsence(String result){
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONObject jsonModel		=	jsonResult.getJSONObject("Model");
			
			String allowanceRemaining	=	jsonModel.getString("AllowanceRemaining");
			String awaitingApproval		=	jsonModel.getString("AwaitingApproval");
			String basicEntitlement		=	jsonModel.getString("BasicEntitlement");
			String booked				=	jsonModel.getString("Booked");
			String braffordFactor		=	jsonModel.getString("BradfordFactor");
			String employeeName			=	jsonModel.getString("EmployeeName");
			String mandatory			=	jsonModel.getString("Mandatory");
			String startDate			=	jsonModel.getString("StartDate");
			String taken				=	jsonModel.getString("Taken");
			String totalEntitlement		=	jsonModel.getString("TotalEntitlement");
			
			
			AbsenceSummary absenceSummary	=	new AbsenceSummary();
			absenceSummary.setAllowanceRemaining(allowanceRemaining);
			absenceSummary.setAwaitingApproval(awaitingApproval);
			absenceSummary.setBasicEntitlement(basicEntitlement);
			absenceSummary.setBooked(booked);
			absenceSummary.setBraffordFactor(braffordFactor);
			absenceSummary.setEmployeeName(employeeName);
			absenceSummary.setMandatory(mandatory);
			absenceSummary.setStartDate(startDate);
			absenceSummary.setTaken(taken);
			absenceSummary.setTotalEntitlement(totalEntitlement);
			
			arrResult.add(absenceSummary);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* this method is used to parse employee information */
	public static ArrayList<Object> parseEmployeeInformation(String result){
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONObject jsonModel		=	jsonResult.getJSONObject("Model");
			
			String 	email			=	jsonModel.getString("Email");
			String	foreName		=	jsonModel.getString("Forename");
			String	homeNumber		=	jsonModel.getString("HomeNumber");
			String 	id				=	jsonModel.getString("Id");
			String	jobTitle		=	jsonModel.getString("JobTitle");
			String	mobileNumber	=	jsonModel.getString("MobileNumber");
			String	orgUnitName		=	jsonModel.getString("OrgUnitName");
			String	startDate		=	jsonModel.getString("StartDate");
			String	surName			=	jsonModel.getString("Surname");
			String	workNumber		=	jsonModel.getString("WorkNumber");
			
			Employees emp			=	new Employees();
			emp.setEmail(email);
			emp.setForeName(foreName);
			emp.setHomeNumber(homeNumber);
			emp.setEmpId(id);
			emp.setJobTitle(jobTitle);
			emp.setMobileNumber(mobileNumber);
			emp.setOrgUnitName(orgUnitName);
			emp.setStartDate(startDate);
			emp.setSurName(surName);
			emp.setWorkNumber(workNumber);
			
			arrResult.add(emp);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* this method is used to parse employee emergency */
	public static ArrayList<Object> parseEmployeeEmergency(String result){
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONArray jsonModel		=	jsonResult.getJSONArray("Model");
			
			ArrayList<EmergencyContact> arrEmergency	=	new ArrayList<EmergencyContact>();
			for(int index = 0; index < jsonModel.length(); index++){
				
				JSONObject item	=	jsonModel.getJSONObject(index);
				
				String name			=	item.getString("Name");
				String relationship	=	item.getString("Relationship");
				String workNumber	=	item.getString("WorkNumber");
				String homeNumber	=	item.getString("HomeNumber");
				String mobileNumer	=	item.getString("MobileNumber");
				
				EmergencyContact emergencyContact	=	new EmergencyContact();
				emergencyContact.setName(name);
				emergencyContact.setRelationship(relationship);
				emergencyContact.setWorkPhone(workNumber);
				emergencyContact.setHomePhone(homeNumber);
				emergencyContact.setMobilePhone(mobileNumer);
				
				arrEmergency.add(emergencyContact);
			}
			
			arrResult.add(arrEmergency);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* this method is used to parse advice number */
	public static ArrayList<Object> parseEmployeeAdviceNumber(String result){
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			String adviceNumber		=	jsonResult.getString("Model");
			
			arrResult.add(adviceNumber);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* =====================================================================
	 * ============================LOG LATENESS PAGE========================
	 * =====================================================================*/
	
	/* this method is used to parse default start end date */
	public static DefaultStartEndTime parseGetDefaultStartEndTime(String result){
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			String empId			=	jsonResult.getString("EmployeeId");
			String defaultStartTime	=	jsonResult.getString("DefaultStartDateTime");
			String defaultEndTime	=	jsonResult.getString("DefaultEndDateTime");
			float days				=	Float.parseFloat(jsonResult.getString("Days"));
			float hours				=	Float.parseFloat(jsonResult.getString("Hours"));
			
			DefaultStartEndTime item	=	new DefaultStartEndTime();
			item.setEmpId(empId);
			item.setDefaultStartTime(defaultStartTime);
			item.setDefaultEndTime(defaultEndTime);
			item.setDay(days);
			item.setHours(hours);
			
			return item;
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	/* this method is used to parse reason lateness */
	public static ArrayList<Object> parseReasonLateness(String result){
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONArray jsonArrayReason	=	jsonResult.getJSONArray("Model");
			ArrayList<ReasonLateness> arrReasonLateness	=	new ArrayList<ReasonLateness>();
			
			for(int index = 0; index < jsonArrayReason.length(); index++){
				
				JSONObject item	=	jsonArrayReason.getJSONObject(index);
				
				String description	=	item.getString("Description");
				String reasonId		=	item.getString("Id");
				
				ReasonLateness reasonLateness	=	new ReasonLateness();
				reasonLateness.setReasonId(reasonId);
				reasonLateness.setDescription(description);
				
				arrReasonLateness.add(reasonLateness);
				
			}
			
			arrResult.add(arrReasonLateness);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* this method is used to parse submit new lateness reason */
	public static Status parseSubmitNewLatenessReason(String result){
		
		Status statusItem = null;
		
		try {
			
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			statusItem				=	Constant.parseStatusModel(jsonStatus);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return statusItem;
	}
	
	/* this method is used to parser get year holiday */
	public static ArrayList<Object> parseGetYearHoliday(String result){
		
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONArray jsonArrayReason	=	jsonResult.getJSONArray("Model");
			ArrayList<HolidayYear> arrHolidayYear	=	new ArrayList<HolidayYear>();
			
			for(int index = 0; index < jsonArrayReason.length(); index++){
				
				JSONObject item	=	jsonArrayReason.getJSONObject(index);
				
				boolean currentHolidayYear	=	item.getBoolean("CurrentHolidayYear");
				String 	endDate				=	item.getString("EndDate");
				String	holidayUnit			=	item.getString("HolidayUnits");
				String	holidayYearId		=	item.getString("HolidayYearId");
				String	name				=	item.getString("Name");
				String	remainingHoliday	=	item.getString("RemainingHoliday");
				String	startDate			=	item.getString("StartDate");
				
				HolidayYear holidayYear	=	new HolidayYear();
				holidayYear.setCurrentHolidayYear(currentHolidayYear);
				holidayYear.setEndDate(endDate);
				holidayYear.setHolidayUnit(holidayUnit);
				holidayYear.setHolidayYearId(holidayYearId);
				holidayYear.setName(name);
				holidayYear.setRemainingHoliday(remainingHoliday);
				holidayYear.setStartDate(startDate);
				
				arrHolidayYear.add(holidayYear);
				
			}
			
			arrResult.add(arrHolidayYear);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* this method is used to parse submit lateness */
	public static Status parseSubmitLateness(String result){
		
		Status statusItem = null;
		
		try {
			
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			statusItem				=	Constant.parseStatusModel(jsonStatus);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return statusItem;
	}
	
	/* this method is used to parse absence type */
	public static ArrayList<Object> parseAbsenceType(String result){
		
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONArray jsonArrayReason	=	jsonResult.getJSONArray("Model");
			ArrayList<AbsenceType> arrAbsenceType	=	new ArrayList<AbsenceType>();
			
			for(int index = 0; index < jsonArrayReason.length(); index++){
				
				JSONObject item	=	jsonArrayReason.getJSONObject(index);
				
				String absenceTypeId	=	item.getString("AbsenceTypeId");
				String color			=	item.getString("Color");
				String description		=	item.getString("Description");
				
				AbsenceType absenceType	=	new AbsenceType();
				absenceType.setAbsenceTypeId(absenceTypeId);
				absenceType.setColor(color);
				absenceType.setDescription(description);
				
				arrAbsenceType.add(absenceType);
			}
			
			arrResult.add(arrAbsenceType);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
		
	}
	
	/* this method is used to parse absence for employee */
	public static ArrayList<Object> parseAbsenceTypeForEmployee(String result){
		
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONArray jsonArrayReason	=	jsonResult.getJSONArray("Model");
			ArrayList<Absence> arrAbsenceType	=	new ArrayList<Absence>();
			
			for(int index = 0; index < jsonArrayReason.length(); index++){
				
				JSONObject item	=	jsonArrayReason.getJSONObject(index);
				
				String absenceId		=	item.getString("AbsenceId");
				String absenceTypeId	=	item.getString("AbsenceTypeId");
				boolean approve			=	item.getBoolean("Approved");
//				boolean conflict		=	item.getBoolean("ConflictingAbsences");
				Float	days			=	Float.parseFloat(item.getString("Days"));
				String	detail			=	item.getString("Details");
				String 	empId			=	item.getString("EmployeeId");
				String	empName			=	item.getString("EmployeeName");
				String	endDate			=	item.getString("EndDate");
				String	holidayYearId	=	item.getString("HolidayYear");
				String	startDate		=	item.getString("StartDate");
				
				Absence absence			=	new Absence();
				absence.setAbsenceId(absenceId);
				absence.setAbsenceTypeId(absenceTypeId);
				absence.setApproved(approve);
//				absence.setConflict(conflict);
				absence.setDays(days);
				absence.setDetail(detail);
				absence.setEmployeeName(empName);
				absence.setEmployeeId(empId);
				absence.setEndDate(endDate);
				absence.setHolidayYearId(holidayYearId);
				absence.setStartDate(startDate);
				
				arrAbsenceType.add(absence);
			}
			
			arrResult.add(arrAbsenceType);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	/* this method is used to parse calculate hour */
	public static ArrayList<Object> parseCalculateHour(String result){
		
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			float days	=	Float.parseFloat(jsonResult.getString("Model"));
			
			arrResult.add(days);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
	}
	
	public static ArrayList<Object> parseLogSicknessSuccess(String result){
		
		ArrayList<Object> arrResult =  new ArrayList<Object>();
		
		try {
			JSONObject jsonResult	=	new JSONObject(result);
			
			JSONObject jsonStatus	=	jsonResult.getJSONObject("Status");
			Status statusItem		=	Constant.parseStatusModel(jsonStatus);
			arrResult.add(statusItem);
			
			JSONObject jsonModel	=	jsonResult.getJSONObject("Model");
			if(jsonModel != null){
				
				JSONArray errorArray	=	jsonModel.getJSONArray("Errors");
				String error			=	"";
				for(int index = 0; index < errorArray.length(); index++){
					error += errorArray.get(index) + "\n";
				}
				
				arrResult.add(error);
				
				JSONArray warningArray	=	jsonModel.getJSONArray("Warnings");
				String warning	=	"";
				for(int index = 0; index < warningArray.length(); index++){
					warning += warningArray.getString(index) + "\n";
				}
				arrResult.add(warning);
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrResult;
		
	}
	
}
